/*
 * LocationFragment.java
 * business
 *
 * Created by SunDing on 2017/1/9.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.location;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseEventFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.ui.loginabout.login.LoginActivity;
import com.yidiandao.business.ui.widget.view.TagFlowLayout;
import com.yidiandao.business.ui.widget.view.TagView;
import com.yidiandao.business.utils.BaiDuSdkHelper;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 首次打开app 定位选择
 */
public class LocationFragment extends SuperBaseEventFragment
        implements LocationContract.View {

    private LocationContract.Presenter mPresenter;
    private TextView mTvLocation;
    private TagFlowLayout mTagLayout;
    private BaiDuSdkHelper mBaiDuSdkHelper;
    private ImageView mIvBack;
    private boolean mIsFromLogin;

    public static LocationFragment getInstance(Bundle extras) {
        LocationFragment fragment = new LocationFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void setPresenter(LocationContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return LocationFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mBaiDuSdkHelper = new BaiDuSdkHelper(mActivity);
        mIvBack = (ImageView) mRootView.findViewById(R.id.iv_back);
        mTvLocation = (TextView) mRootView.findViewById(R.id.tv_location);
        mTagLayout = (TagFlowLayout) mRootView.findViewById(R.id.tag_layout);
        mTagLayout.setTagState(TagView.DISPLAY);
        initEvent();

        mIsFromLogin = getArguments().getBoolean(Intents.IS_FROM_LOGIN);
        mIvBack.setVisibility(mIsFromLogin ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initEvent() {
        mIvBack.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mTagLayout.setTagListener(
                new TagFlowLayout.TagListener() {
                    @Override
                    public void onTagClick(TagView tagView) {
                        if (mBaiDuSdkHelper.getIsLoading()) {
                            mTagLayout.clearAllSelected();
                            return;
                        }
                        mBaiDuSdkHelper.alertDialog(
                                mBaiDuSdkHelper.getCurrentCity(),
                                tagView.getTagDataName(),
                                mTagLayout);
                    }

                    @Override
                    public void onTextChanged(String text) {
                    }
                });
        mTvLocation.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mBaiDuSdkHelper.getCurrentCity())) {
                            mBaiDuSdkHelper.start();
                            mTvLocation.setText("");
                        }
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_location;
    }

    @Override
    public void setCityList(List<Tag> tags) {
        mTagLayout.setTagsData(tags);
    }

    @Override
    public BaiDuSdkHelper getBaiDuUtils() {
        return mBaiDuSdkHelper;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public TextView getLocationTextView() {
        return mTvLocation;
    }

    @Override
    public void notifyDataChanged() {
        if (!mIsFromLogin) {
            LoginActivity.actionStart(mActivity);
        }
        mActivity.finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mBaiDuSdkHelper)
            mBaiDuSdkHelper.closeLocation();
    }
}
